package com.ktjy.controller;

import com.ktjy.service.SysUserService;
import com.pojo.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Date;
import java.util.logging.Logger;

@Controller
@RequestMapping("/user")
@Scope("prototype")
public class SysUserController {
    private Logger logger = Logger.getLogger(SysUserController.class.getName());
    private ArrayList<SysUser> userlist = new ArrayList<>();
    private ArrayList<SysUser> queryUserList = new ArrayList<SysUser>();
    @Resource
    private SysUserService sysUserService;
    @Autowired
    private HttpSession Session;

    @RequestMapping("/tpLogin")
    public String login() {
        return "login";
    }

    @PostMapping("/login")
    public String doLogin(@RequestParam(value = "account") String account, @RequestParam(value = "password") String password) {
        SysUser sysUser = sysUserService.login(account, password);
        if (null != sysUser) {
            return "redirect:/user/index";
        } else {
            return "login";
        }
    }

    @RequestMapping("/toMain")
    public String main() {
        return "frame";
    }

    public SysUserController() {
        try {
            userlist.add(new SysUser(5, "cts", "夏侯涛", "5555555", 1, new Date(), "上海市宝山区", 1, 1, new Date(), 1, new Date()));
            userlist.add(new SysUser(6, "cts", "随风大主播", "66666", 2, new Date(), "上海市陆家嘴", 2, 2, new Date(), 2, new Date()));

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @GetMapping("/list")
    public String list(Model model) {
        logger.info("当前查询条件为空时，查询用户信息" + this);
        queryUserList.clear();
        queryUserList.addAll(userlist);
        model.addAttribute("queryUserList", userlist);
        return "sysUser/sysUserList";
    }

    @PostMapping("/list")
    public String list(@RequestParam(value = "realName", required = false) String realName, Model model) {
        logger.info("查询条件:[realName]=" + realName + ",查询用户信息");
        queryUserList.clear();
        if (realName != null && !realName.equals("")) {
            for (SysUser user : userlist) {
                if (user.getRealName().indexOf(realName) != -1) {
                    queryUserList.add(user);
                }
            }
        } else {
            queryUserList.addAll(userlist);
        }
        model.addAttribute("queryUserList", queryUserList);
        return "sysUser/sysUserList";
    }

    public String doLogin(@RequestParam(value = "account") String account, @RequestParam(value = "password") String password, HttpServletRequest request) {
        SysUser sysUser = sysUserService.login(account, password);
        if (null != sysUser) {
            Session.setAttribute("user", sysUser);
            return "redirect:/user/index";
        } else {
            return "login";
        }
    }

    public String main(HttpSession session) {
        if (session.getAttribute("user") == null) {
            return "redirect:/user/toLogin";
        }
        return "frame";
    }

    @ExceptionHandler(value = {RuntimeException.class})
    public String handlerException(RuntimeException e, HttpServletRequest req) {
        req.setAttribute("e", e);
        return "error";
    }

    public String exLogin(@RequestParam String account, @RequestParam String password) {
        SysUser sysUser = sysUserService.login(account, password);
        if (null != sysUser) {
            throw new RuntimeException("用户名或密码不正确，跳转到错误页面！");
        }
        return "redirect:/user/toMain";
    }

//    public String getUserList(Model model, String queryRealName, Integer queryRoleId, @RequestParam(defaultValue = "1") Integer pageIndex) {
//        List<SysUser> userList = null;
//        try {
//            int pageSize = Constants.pageSize;
//            int totalCount = sysUserService.getUserCount(queryRealName, queryRoleId);
//            PageSupport pages = new PageSupport();
//            pages.setPageSize(pageSize);
//            pages.setTotalCount = pages.getTotalCount();
//            if (pageIndex > totalCount) {
//                pageIndex = totalCount;
//                throw new RuntimeException("页码不正确");
//            }
//            userList
//                    = sysUserService.getUserList(queryRealName, queryRoleId, pageIndex, pageSize);
//            model.addAttribute("userList", userList)
//            List<SysRole> roleList = null;
//            roleList = sysRoleService.getRoleList();
//            model.addAttribute("roleList", roleList);
//            model.addAttribute("queryRealName", queryRealName);
//            model.addAttribute("queryRoleId", queryRoleId);
//            model.addAttribute("pages", pages);
//        } catch (Exception e) {
//            e.printStackTrace();
//            logger.error("获取用户列表失败", e);
//            return "redirect:/user/toSysError";
//        }
//        return "sysUser/list";
//    }
}
